package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcProjectSgazhtgc;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtgcService;

/**
 * 施工安装合同施工过程Controller
 * 
 * @author ruoyi
 * @date 2022-12-11
 */
@Controller
@RequestMapping("/gcgl/sgazhtgc")
public class GcProjectSgazhtgcController extends BaseController
{
    private String prefix = "gcgl/sgazhtgc";

    @Autowired
    private IGcProjectSgazhtgcService gcProjectSgazhtgcService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IGcProjectService gcProjectService;
    
    private static final String FILE_DELIMETER = ",";

    @RequiresPermissions("gcgl:sgazhtgc:view")
    @GetMapping()
    public String sgazhtgc()
    {
        return prefix + "/sgazhtgc";
    }

    /**
     * 查询施工安装合同施工过程列表
     */
    @RequiresPermissions("gcgl:sgazhtgc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSgazhtgc gcProjectSgazhtgc)
    {
        startPage();
        gcProjectSgazhtgc.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcProjectSgazhtgc.setCreateUser(getLoginName());
        }
        List<GcProjectSgazhtgc> list = gcProjectSgazhtgcService.selectGcProjectSgazhtgcList(gcProjectSgazhtgc);
        
        for(GcProjectSgazhtgc obj:list){
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
        		obj.setZilwj(RuoYiConfig.getOnPath()+obj.getZilwj());
        	}
        	
        	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(obj.getSgazhtId());
        	if(gcProjectSgazht!=null){
        		obj.setSupplyName(gcProjectSgazht.getSupplyName());
        	}
        	
        	GcProject gcProject = gcProjectService.selectGcProjectById(obj.getGcProjectid());
        	obj.setCreateUser(gcProject.getOwner());
        }
        
        return getDataTable(list);
    }

    /**
     * 导出施工安装合同施工过程列表
     */
    @RequiresPermissions("gcgl:sgazhtgc:export")
    @Log(title = "施工安装合同施工过程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSgazhtgc gcProjectSgazhtgc)
    {
        List<GcProjectSgazhtgc> list = gcProjectSgazhtgcService.selectGcProjectSgazhtgcList(gcProjectSgazhtgc);
        ExcelUtil<GcProjectSgazhtgc> util = new ExcelUtil<GcProjectSgazhtgc>(GcProjectSgazhtgc.class);
        return util.exportExcel(list, "施工安装合同施工过程数据");
    }

    /**
     * 新增施工安装合同施工过程
     */
    @GetMapping("/add/{sgazhtId}")
    public String add(@PathVariable("sgazhtId") Long sgazhtId, ModelMap mmap){
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(sgazhtId);
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
    	mmap.put("gcProjectid", gcProjectSgazht.getGcProjectid());
    	mmap.put("sgazhtId", sgazhtId);
    	mmap.put("orderNo", gcProjectSgazht.getOrderNo());
    	mmap.put("riq", new Date());
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSgazht.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/add";
    }

    /**
     * 新增保存施工安装合同施工过程
     */
    @RequiresPermissions("gcgl:sgazhtgc:add")
    @Log(title = "施工安装合同施工过程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSgazhtgc gcProjectSgazhtgc){
    	
    	try{
            gcProjectSgazhtgc.setDeleteFlag(1);
            gcProjectSgazhtgc.setCreateAt(new Date());
            gcProjectSgazhtgc.setUpdateAt(new Date());
            gcProjectSgazhtgc.setCreateUser(getLoginName());
            gcProjectSgazhtgc.setUpdateUser(getLoginName());
            gcProjectSgazhtgcService.insertGcProjectSgazhtgc(gcProjectSgazhtgc);
    	}catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 修改施工安装合同施工过程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtgc obj = gcProjectSgazhtgcService.selectGcProjectSgazhtgcById(id);
        if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
        	String[] zlis = obj.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("gcProjectSgazhtgc", obj);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(obj.getGcProjectid());
    	mmap.put("gcProject", gcProject);
    	
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(obj.getSgazhtId());
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
        return prefix + "/edit";
    }
    
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtgc obj = gcProjectSgazhtgcService.selectGcProjectSgazhtgcById(id);
        if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
        	String[] zlis = obj.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("gcProjectSgazhtgc", obj);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(obj.getGcProjectid());
    	mmap.put("gcProject", gcProject);
    	
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(obj.getSgazhtId());
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
        return prefix + "/detail";
    }

    /**
     * 修改保存施工安装合同施工过程
     */
    @Log(title = "施工安装合同施工过程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSgazhtgc gcProjectSgazhtgc){
    	
    	try{
            gcProjectSgazhtgc.setUpdateAt(new Date());
        	gcProjectSgazhtgc.setUpdateUser(getLoginName());
            gcProjectSgazhtgcService.updateGcProjectSgazhtgc(gcProjectSgazhtgc);
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 删除施工安装合同施工过程
     */
    @RequiresPermissions("gcgl:sgazhtgc:remove")
    @Log(title = "施工安装合同施工过程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSgazhtgcService.deleteGcProjectSgazhtgcByIds(ids));
    }
    
    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception{
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}